#include "mainwindow.h"
#include "configdialog.h"
#include "ui_mainwindow.h"
#include "../src/CCreateWorld.h"

using namespace Qt;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
	QObject::connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(GameStartup()));
	QObject::connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(ShowConfigDialog()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::GameStartup()
{
	this->hide();
	CCreateWorld World;
	ConfigContainer configContainer;

	// Now we check if the config file exists - if it does not, we create one with default values.
	getConfig(&configContainer);

	World.SpawnPrototypeWorld(&configContainer);
}

void MainWindow::ShowConfigDialog()
{
	ConfigDialog cd;
	ConfigContainer configContainer;
	getConfig(&configContainer);
	cd.setConfig(&configContainer);

	cd.exec();
}

/// Read all config files and fill the config structs
/**
	With this function, we read all .cfg files located at PATH/TO/EXE/config/*.cfg and fill the 
	config structures with the read data. If the files do not exist, we create those files and fill them
	with default values.

	\param configContainer A pointer to the ConfigContainer-struct that holds all config structs. (This struct should be created in GameStartup()

	\remarks This function should be called just before the game startup, since the ConfigContainer gets delivered to the game engine
*/
void MainWindow::getConfig(ConfigContainer *configContainer)
{
	QDir qdirDir;
	QTextStream qtxtstrm;
	QFile qfileVideoConfigFile("config/video.cfg");
	QFile qfileInputConfigFile("config/input.cfg");

	qdirDir.mkpath("config"); // create the path

	/*******************************************************************
	****************************** Video *******************************
	********************************************************************/
	// First we check if we have a config file - if not, we create one with default values
	if(!qfileVideoConfigFile.exists())
	{
		qfileVideoConfigFile.open(QIODevice::WriteOnly);
		qtxtstrm.setDevice(&qfileVideoConfigFile);

		qtxtstrm << "strResolutionX" << " " << "1024" << "\n";
		qtxtstrm << "strResolutionY" << " " << "768" << "\n";
		qtxtstrm << "bFullscreen" << " " << "false" << "\n";
		qtxtstrm << "strRenderSystem" << " " << "OpenGL" << "\n";
		qtxtstrm << "bVSync" << " " << "false" << "\n";
		qtxtstrm << "iBPP" << " " << "16" << "\n";

		qfileVideoConfigFile.close();
	}

	// Now we get our values out of the config file	
	qfileVideoConfigFile.open(QIODevice::ReadOnly | QIODevice::Text);
	qtxtstrm.setDevice(&qfileVideoConfigFile);

	while(!qtxtstrm.atEnd())
	{
		QString strLine = qtxtstrm.readLine();
		
		QString strProperty = strLine.mid(0, strLine.indexOf(" ") );
		QString strValue = strLine.mid( strLine.indexOf(" ") + 1 );

		if(strProperty.compare("strResolutionX") == 0)
		{
			configContainer->videoConfig.iResolutionX = strValue.toInt();
		}
		else if(strProperty.compare("strResolutionY") == 0)
		{
			configContainer->videoConfig.iResolutionY = strValue.toInt();
		}
		else if(strProperty.compare("bFullscreen") == 0)
		{
			(strValue.compare("true",CaseInsensitive) == 0) ? (configContainer->videoConfig.bFullscreen = true) : (configContainer->videoConfig.bFullscreen = false);
		}
		else if(strProperty.compare("strRenderSystem") == 0)
		{
			strcpy_s(configContainer->videoConfig.carrRenderMode, 16, strValue.toStdString().c_str());
		}
		else if(strProperty.compare("bVSync") == 0)
		{
			(strValue.compare("true",CaseInsensitive) == 0) ? (configContainer->videoConfig.bVSync = true) : (configContainer->videoConfig.bVSync = false);
		}
		else if(strProperty.compare("iBPP") == 0)
		{
			configContainer->videoConfig.iBPP = strValue.toInt();
		}
		// We just ignore everything else
	}

	/*****************************************************************************
	*********************************** Input ************************************
	******************************************************************************/
	// First we check if we already have a file. 
	// If not, than we create one with default values
	if(!qfileInputConfigFile.exists())
	{
		qfileInputConfigFile.open(QIODevice::WriteOnly);
		qtxtstrm.setDevice(&qfileInputConfigFile);

		qtxtstrm << "cForward w\n";
		qtxtstrm << "cBackward s\n";
		qtxtstrm << "cUp SPACE\n";
		qtxtstrm << "cDown LCTRL\n";
		qtxtstrm << "cLeft a\n";
		qtxtstrm << "cRight d\n";
		qtxtstrm << "cRun Shift\n";
		qtxtstrm << "cMenu Escape\n";

		qfileInputConfigFile.close();
	}

	qfileInputConfigFile.open(QIODevice::ReadOnly | QIODevice::Text);
	qtxtstrm.setDevice(&qfileInputConfigFile);

	while(!qtxtstrm.atEnd())
	{
		QString strLine = qtxtstrm.readLine();

		QString strProperty = strLine.mid(0, strLine.indexOf(" "));
		QString strValue = strLine.mid(strLine.indexOf(" ") + 1);
		//if(strProperty.compare("") == 0)
		//{
		//	break;
		//}
		if(strProperty.compare("cForward") == 0)
		{
			strcpy_s(configContainer->keyMapping.carrForward, 6, strValue.toStdString().c_str());
		}
		else if(strProperty.compare("cBackward") == 0)
		{
			strcpy_s(configContainer->keyMapping.carrBackward, 6, strValue.toStdString().c_str());
		}
		else if(strProperty.compare("cUp") == 0)
		{
			strcpy_s(configContainer->keyMapping.carrUp, 6, strValue.toStdString().c_str());
		}
		else if(strProperty.compare("cDown") == 0)
		{
			strcpy_s(configContainer->keyMapping.carrDown, 6, strValue.toStdString().c_str());
		}
		else if(strProperty.compare("cLeft") == 0)
		{
			strcpy_s(configContainer->keyMapping.carrLeft, 6, strValue.toStdString().c_str());
		}
		else if(strProperty.compare("cRight") == 0)
		{
			strcpy_s(configContainer->keyMapping.carrRight, 6, strValue.toStdString().c_str());
		}
		else if(strProperty.compare("cRun") == 0)
		{
			strcpy_s(configContainer->keyMapping.carrRun, 6, strValue.toStdString().c_str());
		}
		else if(strProperty.compare("cMenu") == 0)
		{
			strcpy_s(configContainer->keyMapping.carrMenu, 6, strValue.toStdString().c_str());
		}
	}
}